# coding:utf8
from typing import List
from utils.embedding.base_embedding import BaseEmbeddings

dashscope_api_key = "sk-cb4016a8cf3943d799710a03a8993e61"

class DashscopeEmbedding(BaseEmbeddings):
    def __init__(self, path: str = '', is_api: bool = True) -> None:
        super().__init__(path, is_api)
        if self.is_api:
            # 初始化 dashscope API 客户端
            from langchain_community.embeddings import DashScopeEmbeddings
            self.client = DashScopeEmbeddings(
                model="text-embedding-v1", dashscope_api_key=dashscope_api_key
            )
    
    def get_embedding(self, text: str, model: str = "text-embedding-v1") -> List[float]:
        if self.is_api:
            # 去掉文本中的换行符，保证输入格式规范
            text = text.replace("\n", " ")
            # 调用 Dashscope API 获取文本的向量表示
            return self.client.embed_query(text)
        else:
            raise NotImplementedError  # 如果不是 API 模式，这里未实现本地模型的处理